package com.example.wordpress;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

public class EditorView extends View {
	
	private final Activity postEditorActivity;
	private ProgressDialog pDialog;
	
	public EditorView(Context context) {
		super(context);
		postEditorActivity = (Activity) context;
		postEditorActivity.setContentView(R.layout.activity_editor);
		pDialog = createProgressDialog();
	}
	
	public boolean createOptionsMenu(Menu menu) {
		MenuInflater inflater = postEditorActivity.getMenuInflater();
		inflater.inflate(R.menu.editor, menu);
		return true;
	}
	
	public Activity getActivity() {
		return postEditorActivity;
	}
	
	public CheckBox getFacebookCheckBox() {
		return (CheckBox) postEditorActivity.findViewById(R.id.check_facebook);
	}
	
	public String getPostTitle() {
		return ((EditText) postEditorActivity.findViewById(R.id.edit_title)).getText().toString();
	}

	public void setPostTitle(String title) {
		((EditText) postEditorActivity.findViewById(R.id.edit_title)).setText(title);
	}
	
	public String getPostContent() {
		return ((EditText) postEditorActivity.findViewById(R.id.edit_content)).getText().toString();
	}
	
	public void setPostContent(String content) {
		((EditText) postEditorActivity.findViewById(R.id.edit_content)).setText(content);
	}
	
	public String getPostExcerpt() {
		return ((EditText) postEditorActivity.findViewById(R.id.edit_excerpt)).getText().toString();
	}
	
	public void setPostExcerpt(String excerpt) {
		((EditText) postEditorActivity.findViewById(R.id.edit_excerpt)).setText(excerpt);
	}
	
	public String getStatus() {
		Spinner spinner = (Spinner) postEditorActivity.findViewById(R.id.select_status);
		String selected = spinner.getSelectedItem().toString();
		String result = "";
		
		if(selected.contentEquals("Publish"))
			result = "publish";
		else if(selected.contentEquals("Draft"))
			result = "draft";
		else if(selected.contentEquals("Pending Review"))
			result = "pending";
		else if(selected.contentEquals("Private"))
			result = "private";
		else if(selected.contentEquals("Trash"))
			result = "trash";
		
		return result;
	}
	
	public void setPostStatus(String status) {
		Spinner spinner = (Spinner) postEditorActivity.findViewById(R.id.select_status);
		
		int position = 0;
		
		if(status.contentEquals("publish"))
			position = 0;
		else if(status.contentEquals("draft"))
			position = 1;
		else if(status.contentEquals("pending"))
			position = 2;
		else if(status.contentEquals("private"))
			position = 3;
		else if(status.contentEquals("trash"))
			position = 4;
		
		spinner.setSelection(position);
	}
	
	/**
	 * Crea un di�logo de progreso.
	 * @return Devuelve la instancia del di�logo creado.
	 */
	
	public ProgressDialog createProgressDialog() {
		ProgressDialog pd = new ProgressDialog(postEditorActivity);
		pd.setTitle("Processing...");
		pd.setMessage("Please wait.");
		pd.setCancelable(false);
		pd.setIndeterminate(true);
		pd.hide();
		return pd;
	}
	
	/**
	 * Muestra el di�logo de progreso asociado a MainActivity
	 */
	
	public void showProgressDialog() {
		pDialog.show();
	}
	
	/**
	 * Oculta el di�logo de progreso asociado a MainActivity
	 */
	
	public void hideProgressDialog() {
		pDialog.hide();
	}
	
	public void dismissProgressDialog() {
		pDialog.dismiss();
	}

	public void enableMenuDeletePost() {
		postEditorActivity.findViewById(R.id.menu_delete_post).setEnabled(true);
	}
}
